﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class CourseCommentBLL : CommonBLL<CourseCommentInfo, CourseCommentDAL>
    {
        public static string GetStuName(CourseCommentInfo info)
        {
            if (info.ReviewerId == 0 || info.StudentInfo == null)
                return info.ReviewerName;
            return info.StudentInfo.StuName;
        }

        public static string GetTeaName(CourseCommentInfo info)
        {
            if (info.TeacherInfo == null)
                return info.TeacherName;
            return info.TeacherInfo.Name;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type">0教师评价1课程评价</param>
        /// <param name="teacherId">0全部</param>
        /// <param name="reviewerType">-1全部评价0学生评价1砖家评价</param>
        /// <param name="rank"></param>
        /// <returns></returns>
        public static IQueryable<CourseCommentInfo> GetList(int type, int teacherId, int reviewerType, int rank)
        {
            var re = GetList();
            re = re.Where(o => o.CommentType == type);
            //教师评价
            if (type == 0)
            {
                if (teacherId != 0)
                    re = re.Where(o => o.TeacherId == teacherId);
            }
            if (reviewerType != -1)
                re = re.Where(o => o.ReviewerType == reviewerType);
            if (rank != 0)
                re = re.Where(o => o.Rank == rank);
            return re;
        }

        public static CourseCommentInfo Get(int id)
        {
            using (var dal = new CourseCommentDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new CourseCommentDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }
    }
}
